import vue from '@/main';
import WebIM from '../utils/WebIM';

const Friends = {
    state: {
        // 拒绝申请的用户
        refuseFriend: '',
        // 同意申请的用户
        agreeFriend: '',
        // 没找到的用户
        noFindId: '',
        // 是否有来信
        mesg: false,
        // 消息条数
        count: 0,
        // 朋友的基本信息
        friendsBasicObj: {},
        // 同意申请的用户集
        friendsApplyArr: [],
        // 朋友的id
        friendsArr: [],
        // 所有朋友的基本信息
        friendsAllBasicArr: []
    },
    mutations: {
        // 搜索列表的朋友信息
        searchFirendsBasic(state, payload) {
            state.friendsBasicObj = payload;
        },
        // cleanFirendsBasic(state) {
        //     state.friendsBasicObj = {};
        // },
        // 修改请求列表
        updateFriendsApplyArr(state, payload) {
            state.friendsApplyArr.push(payload);
        },
        // 修改朋友列表
        updateFriendsArr(state, payload) {
            state.friendsArr.push(payload);
        },
        // 修改所有朋友的基本信息列表
        searchAllFirendsBasic(state, payload) {
            state.friendsAllBasicArr.push(payload);
        },
        // 删除所有朋友的基本信息列表
        delAllFirendsBasic(state) {
            state.friendsAllBasicArr = [];
        },
        refuseFriendId(state, payload) {
            state.refuseFriend = payload;
        },
        agreeFriendId(state, payload) {
            state.agreeFriend = payload;
        },
        noFindFriendsId(state, payload) {
            state.noFindId = payload;
        },
        hasMesg(state) {
            state.mesg = true;
        },
        delMesg(state) {
            state.mesg = false;
        },
        addCount(state) {
            console.log(state.count);
            state.count++;
            console.log(state.count);
        },
        cleanCount(state) {
            state.count = 0;
        },
        cleanFriendsApplyArr(state) {
            state.friendsApplyArr = [];
        }

    },
    actions: {
        // 通过id搜索好友
        searchFriends(context, payload) {
            // 判断是否是自己的id

            if (payload === context.rootState.login.userId) {
                context.commit('noFindFriendsId', payload);
            } else {

                WebIM.conn.getContacts().then((res) => {

                    // 判断朋友列表是否为空
                    if (res.data.length === 0) {
                        WebIM.conn.fetchUserInfoById(payload).then((res) => {
                            console.log(res, 'hahha' + JSON.stringify(res.data[payload]));
                            if (JSON.stringify(res.data[payload]) != '{}') {
                                let basicObj = {
                                    nickname: res.data[payload].nickname,
                                    avatarurl: res.data[payload].avatarurl,
                                    phone: res.data[payload].phone,
                                    isFriends: false
                                }
                                console.log(basicObj, "111111");
                                context.commit('searchFirendsBasic', basicObj);
                            }
                            else {
                                console.log(22222);
                                // context.commit('cleanFirendsBasic');
                                context.commit('noFindFriendsId', payload);
                            }

                        }).catch(error => {
                            console.log(error);
                            // context.commit('cleanFirendsBasic');
                            context.commit('noFindFriendsId', payload);
                        })
                    }
                    else {
                        let arr = res.data;
                        let basicObj = {};
                        let flag = false;
                        arr.map((item) => {
                            if (item == payload) {
                                flag = true
                            }
                        })

                        WebIM.conn.fetchUserInfoById(payload).then((res) => {
                            console.log(res);
                            if (JSON.stringify(res.data[payload]) !== '{}') {
                                // let friendsBasic = JSON.parse(res.data[payload].ext);
                                basicObj = {
                                    nickname: res.data[payload].nickname,
                                    avatarurl: res.data[payload].avatarurl,
                                    phone: res.data[payload].phone,
                                    isFriends: true
                                }
                                if (flag) {
                                    context.commit('searchFirendsBasic', basicObj);
                                } else {
                                    basicObj.isFriends = false
                                    context.commit('searchFirendsBasic', basicObj);
                                }
                            } else {
                                // context.commit('cleanFirendsBasic');
                                context.commit('noFindFriendsId', payload);
                            }
                        }).catch(error => {
                            console.log(error);
                            // context.commit('cleanFirendsBasic');
                            context.commit('noFindFriendsId', payload);
                        })
                    }
                })
            }
        },
        // 发送申请信息给朋友
        sendMsgToFriends(context, { id, text }) {
            WebIM.conn.addContact(id, text);
        },
        // 更改申请列表信息
        updateFriendsApply(context, { id, text }) {
            context.commit('updateFriendsApplyArr', {
                id,
                text
            });
        },
        // 同意请求
        agreeApplyFromFriends(context, payload) {
            console.log(payload);
            WebIM.conn.acceptContactInvite(payload);
        },
        // 拒绝请求
        refuseApplyFromFriends(context, payload) {
            console.log(payload);
            WebIM.conn.declineContactInvite(payload);
        },
        // 修改通讯录列表
        updateFriendsList(context, payload) {
            context.commit('updateFriendsArr', payload)
        },
        // 搜索所有的朋友
        searchAllFriends(context) {
            WebIM.conn.getContacts().then((res) => {
                let arr = res.data;
                arr.forEach(item => {

                    WebIM.conn.fetchUserInfoById(item).then((res) => {
                        console.log(res);
                        // let friendsBasic = JSON.parse(res.data[item].ext);
                        let basicObj = {
                            nickname:  res.data[item].nickname,
                            avatarurl: res.data[item].avatarurl,
                            phone: res.data[item].phone
                        }
                        console.log(basicObj);
                        context.commit('searchAllFirendsBasic', basicObj);
                    }).catch(error => {
                        console.log(error);
                    })
                })
            })
        },
        // 拒绝申请的用户的id
        refuseFriendsList(context, payload) {
            context.commit('refuseFriendId', payload);
        },
        // 同意申请的用户的id
        agreeFriendsList(context, payload) {
            context.commit('agreeFriendId', payload);
        },
    },
    getters: {}
}

export default Friends;